package user_api

import (
	"errors"
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

func (u UserApi) LogoutView(c *gin.Context) {
	token := c.GetHeader("token")
	var userLogin models.UserLoginModel
	where := global.DB.Where("token = ?", token)
	err := global.DB.Where(where).Take(&userLogin).Error
	if err != nil {
		err = errors.New("记录不存在")
		return
	}
	global.DB.Model(&userLogin).Update("lose", true)
	//获取token，将token，加入redis，设置token的过期时间
	claims, _ := jwts.ParseToken(token)
	err = u.RedisService.SetLogoutToken(ctype.UserHandleLogout, token, claims.ExpiresAt)
	if err != nil {
		res.FailWithMsg(c, "注销失败")
		return
	}
	res.OkWithMsg(c, "注销成功")
}
